package com.jianshuge.app.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jianshuge.app.AppException;
import com.jianshuge.app.common.StringUtils;

public class BehaviorList extends Entity {
	public final static int CATALOG_HOT_BOOKLIST = 1;
	public final static int CATALOG_ZERO_BOOKLIST = 2;
	public final static int CATALOG_FOLLOWED_BOOKLIST = 3;
	public final static int CATALOG_CREATED_BOOKLIST = 4;
	public final static int CATALOG_PART_BOOKLIST = 5;
	
	private int page_size;
	private int behavior_count;
	
	private List<Behavior> behavior_list = new ArrayList<Behavior>();
	
	public int getPageSize() {
		return page_size;
	}
	public int getBehaviorCount() {
		return behavior_count;
	}
	public List<Behavior> getBehaviorList() {
		return behavior_list;
	}
	
	public static BehaviorList parse(InputStream inputStream) throws IOException, AppException {
		BehaviorList my_behavior_list = new BehaviorList();
		Behavior behavior = null;
		
		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
	    
	    try {
	    
	    	JSONArray json_arr = new JSONArray(strResult);
		
	    	my_behavior_list.page_size = json_arr.length();
	    	my_behavior_list.behavior_count = json_arr.length();

	    	for (int i = 0; i < json_arr.length(); i++) {
	    		
	    		behavior = new Behavior();
	    		JSONObject behavior_json_obj = json_arr.optJSONObject(i);
	    	
	    		behavior.id = StringUtils.toInt(behavior_json_obj.optString("id"), 0);
	    	    behavior.setUserId( behavior_json_obj.optInt("user_id") );
	    	    behavior.setUserName(behavior_json_obj.optString("user_name"));
	    	    behavior.setUserAvatar(behavior_json_obj.optString("user_avatar"));
	    	    
	    	    int action_type = StringUtils.toInt(behavior_json_obj.optString("action_type"), 0);
	    	    behavior.setActionType( action_type );
	    	    behavior.setAction(behavior_json_obj.optString("action"));
	    	    
	    	    behavior.setTargetId(behavior_json_obj.optInt("target_id"));
	    	    behavior.setTargetTitle(behavior_json_obj.optString("target_title"));
	    	    behavior.setTargetUrl(behavior_json_obj.optString("target_url"));
	    	    behavior.setTargetImgUrl(behavior_json_obj.optString("target_img_url"));
	    	    
	    	    behavior.setTargetPid(behavior_json_obj.optInt("target_pid"));
	    	    behavior.setTargetPtitle(behavior_json_obj.optString("target_ptitle"));    
	    	    behavior.setTime(behavior_json_obj.optString("time"));
	     	
	    	    String event = behavior_json_obj.optString("event_type");
	    	    
	    	    if( event.isEmpty() )
	    	    {
	    	    	/************************************* 
	    	    	 ** 2：关注书单 4：创建书单 6：评论书单 
	    	    	 *************************************/ 
	    	    	if( action_type == 2 || action_type == 4 || action_type == 6 ) {
	    		
	    	    		JSONArray booklistitems_json_objs = behavior_json_obj.optJSONArray("booklistitems");
	    	    		if(booklistitems_json_objs != null) {
	    	    			for (int j = 0; j < booklistitems_json_objs.length(); j++) {
	    	    				BooklistItem booklist_item = new BooklistItem();
	    	    				JSONObject booklist_item_json_obj = booklistitems_json_objs.optJSONObject(j);
	    	    				booklist_item.setBookId( booklist_item_json_obj.optInt("book_id") );
	    	    				booklist_item.setTitle( booklist_item_json_obj.optString("book_title") );
	    	    				booklist_item.setBookImageUrl( booklist_item_json_obj.optString("book_image_url") );
	    	    				booklist_item.setBookRating( booklist_item_json_obj.optString("book_rating") );
	    	    				behavior.getBooklistItems().add(booklist_item);
	    	    			}
	    	    		}
					
	    	    		JSONArray contributor_json_objs = behavior_json_obj.optJSONArray("contributors");
	    	    		if( contributor_json_objs != null ) {
	    	    			for (int k = 0; k < contributor_json_objs.length(); k++) {
	    	    				Contributor contributor = new Contributor();
	    	    				JSONObject contributor_json_obj = contributor_json_objs.optJSONObject(k);
	    	    				contributor.setUserId( contributor_json_obj.optString("user_id") );
	    	    				contributor.setUserName( contributor_json_obj.optString("user_name") );
	    	    				contributor.setUserAvatar( contributor_json_obj.optString("user_avatar") );	
	    	    				behavior.getContributors().add(contributor);
	    	    			}	
	    	    		}
	    	    	}
	    	    }
	    	    else
	    	    {
	    	    	//发送通知时使用
	    	    	 /************************************* 
		    	     ** 1：关注了你创建的书单  4：评论了你创建的书单 5：评论了你关注的书单 
		    	     *************************************/ 
		    		if( action_type == 1 || action_type == 4 || action_type == 5 ) {
			    		
		    			JSONArray booklistitems_json_objs = behavior_json_obj.optJSONArray("booklistitems");
		    			if(booklistitems_json_objs != null) {
		    				for (int j = 0; j < booklistitems_json_objs.length(); j++) {
		    					BooklistItem booklist_item = new BooklistItem();
		    					JSONObject booklist_item_json_obj = booklistitems_json_objs.optJSONObject(j);
		    					booklist_item.setBookId( booklist_item_json_obj.optInt("book_id") );
		    					booklist_item.setTitle( booklist_item_json_obj.optString("book_title") );
		    					booklist_item.setBookImageUrl( booklist_item_json_obj.optString("book_image_url") );
		    					booklist_item.setBookRating( booklist_item_json_obj.optString("book_rating") );
		    					behavior.getBooklistItems().add(booklist_item);
		    				}
		    			}
						
						JSONArray contributor_json_objs = behavior_json_obj.optJSONArray("contributors");
						if( contributor_json_objs != null ) {
							for (int k = 0; k < contributor_json_objs.length(); k++) {
								Contributor contributor = new Contributor();
								JSONObject contributor_json_obj = contributor_json_objs.optJSONObject(k);
								contributor.setUserId( contributor_json_obj.optString("user_id") );
								contributor.setUserName( contributor_json_obj.optString("user_name") );
								contributor.setUserAvatar( contributor_json_obj.optString("user_avatar") );	
								behavior.getContributors().add(contributor);
							}	
						}
		    		}
	    	    	
	    	    }
	    	    
	    		/***********************************************
	    		 ** 5：参与书单 7：评论书单条目 8：投票书单条目
	    		 ***********************************************/
	    		if( action_type == 5 || action_type == 7 || action_type == 8 ) {
	    			behavior.setTargetImgUrl(behavior_json_obj.optString("target_img_url"));
	    			behavior.setTargetPtitle(behavior_json_obj.optString("target_ptitle"));
	    		}
	    		
	    		/******************************
	    	  	 ** 0：关注人 12：收藏了书籍 
	    		 ******************************/
	    		behavior.setTargetImgUrl(behavior_json_obj.optString("target_img_url"));
	    		
	    		my_behavior_list.getBehaviorList().add(behavior);
	    		behavior = null;
	    		
	    	}

		
		} catch (JSONException e) { 
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        }
		
		return my_behavior_list;
	}

}
